package org.dromara.system.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.GzhUserBo;
import org.dromara.system.domain.vo.GzhUserVo;
import org.dromara.system.domain.GzhUser;
import org.dromara.system.mapper.GzhUserMapper;
import org.dromara.system.service.IGzhUserService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 公众号关注用户Service业务层处理
 *
 * @author Lion Li
 * @date 2024-06-27
 */
@RequiredArgsConstructor
@Service
public class GzhUserServiceImpl implements IGzhUserService {


    private final GzhUserMapper baseMapper;

    /**
     * 查询公众号关注用户
     */
    @Override
    public GzhUserVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询公众号关注用户列表
     */
    @Override
    public TableDataInfo<GzhUserVo> queryPageList(GzhUserBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<GzhUser> lqw = buildQueryWrapper(bo);
        Page<GzhUserVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询公众号关注用户列表
     */
    @Override
    public List<GzhUserVo> queryList(GzhUserBo bo) {
        LambdaQueryWrapper<GzhUser> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<GzhUser> buildQueryWrapper(GzhUserBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<GzhUser> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getUnionid()), GzhUser::getUnionid, bo.getUnionid());
        lqw.eq(StringUtils.isNotBlank(bo.getOpenid()), GzhUser::getOpenid, bo.getOpenid());
        lqw.eq(bo.getSubscribe() != null, GzhUser::getSubscribe, bo.getSubscribe());
        lqw.eq(bo.getSubscribeTime() != null, GzhUser::getSubscribeTime, bo.getSubscribeTime());
        lqw.like(StringUtils.isNotBlank(bo.getNickname()), GzhUser::getNickname, bo.getNickname());
        lqw.eq(bo.getSex() != null, GzhUser::getSex, bo.getSex());
        lqw.eq(StringUtils.isNotBlank(bo.getCountry()), GzhUser::getCountry, bo.getCountry());
        lqw.eq(StringUtils.isNotBlank(bo.getProvince()), GzhUser::getProvince, bo.getProvince());
        lqw.eq(StringUtils.isNotBlank(bo.getCity()), GzhUser::getCity, bo.getCity());
        lqw.eq(StringUtils.isNotBlank(bo.getLanguage()), GzhUser::getLanguage, bo.getLanguage());
        lqw.eq(StringUtils.isNotBlank(bo.getHeadimgurl()), GzhUser::getHeadimgurl, bo.getHeadimgurl());
        lqw.eq(bo.getGroupid() != null, GzhUser::getGroupid, bo.getGroupid());
        return lqw;
    }

    /**
     * 新增公众号关注用户
     */
    @Override
    public Boolean insertByBo(GzhUserBo bo) {
        GzhUser add = MapstructUtils.convert(bo, GzhUser.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改公众号关注用户
     */
    @Override
    public Boolean updateByBo(GzhUserBo bo) {
        GzhUser update = MapstructUtils.convert(bo, GzhUser.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(GzhUser entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除公众号关注用户
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public GzhUserVo queryList1(GzhUserBo gzhUserBo) {
        LambdaQueryWrapper<GzhUser> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(gzhUserBo.getOpenid()), GzhUser::getOpenid, gzhUserBo.getOpenid());
        return baseMapper.selectVoOne(lqw);
    }

    @Override
    public GzhUserVo queryGzhById(Long userId) {
        return baseMapper.selectUserVoById();
    }


}
